package Towers;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import Main.GamePanel;

public class Tower {
	// posicionamento
	public int x;
	public int y; 
	public int width;
	public int height;
	// atributos 
	public float life;
	public float range;
	public float demange;
	public int custo;
	// variaveis para desenho
	public BufferedImage imagemDemonstracao;
	public BufferedImage imagemNoCenario;
	public Boolean selecionada;
	public Color cor;
	public boolean fixada;
	public boolean visible;
	

	public Tower(int posX, int posY, int Width, int Height, BufferedImage img) {
		// TODO Auto-generated constructor stub
		
		x = posX;
		y = posY;
		width = Width;
		height = Height;
		imagemNoCenario = img;
		selecionada = false;
		cor = Color.BLUE;		
		fixada = false;
		visible = true;
		range = 50;
		
		
		
	}
	
	public void constroiTorre(int tipo){
		switch(tipo){
		case 0:{
			demange = 1;
			life = 5;
			range += 5;
			custo= 10;
			break;
		}
		case 1:{
			demange = 1;
			life = 8;
			range += 10;
			custo= 10;
			break;
		}
		case 2:{
			demange = 2;
			life = 8;
			range += 5;
			custo= 10;
			break;
		}
		case 3:{
			demange = 2;
			life = 8;
			range += 10;
			custo= 10;
			break;
		}
		case 4:{
			demange = 2;
			life = 9;
			range += 25;
			custo= 10;
			break;
		}
		case 5:{
			demange = 3;
			life = 15;
			range += 80;
			custo= 10;
			break;
		}
		case 6:{
			demange = 3;
			life = 15;
			range = 8;
			custo= 10;
			break;
		}
		case 7:{
			demange = 1;
			life = 8;
			range = 2;
			custo= 10;
			break;
		}
			
		}
	}
	
	public void Draw(Graphics2D dbg){
		dbg.setColor(cor);		
		dbg.fillRect(x-width/2, y-height/2, width, height);
		dbg.drawOval((int) (x-range), (int) (y-range), (int)(2*range)  ,(int)(2*range) );
		dbg.setColor(Color.BLACK);
		dbg.drawLine(x-5, y, x+5, y);
		dbg.drawLine(x, y-5, x, y+5);
		
		
	}
	
	public void Update(long diffTime){
		
	}
	
	public void posiciona(int posX, int posY) {
		this.x = posX;
		this.y = posY;
	}
	
	@SuppressWarnings("static-access")
	public void mouseMove(MouseEvent e)	{
		if(selecionada){
			x = e.getX();
			y = e.getY();
			
			
			if(x+16<720 && x-16>0 && y+16<480 && y-16>0){
				
				int valores[] = new int[4];
				valores[0] = GamePanel.instance.MAPA[0].mapa2[(y-height/2)/16][(x-width/2)/16];
				valores[1] = GamePanel.instance.MAPA[0].mapa2[(y-height/2)/16][(x+width/2)/16];
		        valores[2] = GamePanel.instance.MAPA[0].mapa2[(y+height/2)/16][(x-width/2)/16];
                valores[3] = GamePanel.instance.MAPA[0].mapa2[(y+height/2)/16][(x+width/2)/16];
				//System.out.println("valores: " + valores[0] + ", " + valores[1] + ", " + valores[2] + ", " + valores[3]);
				if(valores[0]==0 && valores[1]==0 && valores[2]==0 && valores[3]==0){
					
					cor = Color.GREEN;
					
				}else{
					cor =Color.RED;
				}
			}else{
				
				cor = Color.BLUE;
			}
		}
	}
	
	@SuppressWarnings("static-access")
	public void mouseClik(MouseEvent e){
		if(!fixada){
			if(!selecionada){
				if(Utils.Utilidades.ColisaoRetangular(e.getX(), e.getY(), e.getX()+1, e.getY()+1, this.x-16, this.y-16, this.x+width-16, this.y+height-16)){
					selecionada= true;
				}
			}else{
				if(selecionada){
					if(cor == Color.GREEN){
						fixada = true; 
						selecionada = false;
					}
					
				}
			}
		}
	}

}
